﻿Namespace Commands

    Partial Class GetShareItems
        Private Class GetFiles
            Friend Shared Function GetReply(ByVal share As Entities.Share, ByVal directoryCode As String) As Reply
                Dim clsRet As Reply = Nothing
                If String.IsNullOrEmpty(directoryCode) Or directoryCode = "\" Then
                    clsRet = CreateReplyObject(True, False)
                    Dim clsGroup As New Group("Shared Files")
                    clsRet.Groups.Add(clsGroup)
                    clsGroup.Directories.Add(New Directory("\FileSys", "Browse by path", "Browses files in the hierarcical structure on disk at your buddy"))
                    clsGroup.Directories.Add(New Directory("\NewLast7Days", "Recent Additions (7 days)", "New files during the last week"))
                    clsGroup.Directories.Add(New Directory("\NewLast31Days", "Recent Additions (31 days)", "New files during the last month"))

                ElseIf Not directoryCode.StartsWith("\"c) Then
                    clsRet = CreateReplyObject(False, False)
                    'erranous directory, must start with "\"
                Else
                    Dim aParts() As String = directoryCode.Substring(1).Split("\"c)
                    Select Case aParts(0).ToLower
                        Case "filesys"
                            clsRet = GetByFileSys(share, directoryCode, aParts)
                        Case "newlast7days"
                            clsRet = GetRecent(share, Now.ToUniversalTime.Subtract(TimeSpan.FromDays(7)))
                        Case "newlast31days"
                            clsRet = GetRecent(share, Now.ToUniversalTime.Subtract(TimeSpan.FromDays(31)))
                        Case Else
                            clsRet = CreateReplyObject(False, False)
                    End Select
                End If
                Return clsRet
            End Function

            Private Shared Function GetByFileSys(ByVal share As Entities.Share, ByVal directoryCode As String, ByVal parts() As String) As Reply
                Dim clsRet As Reply = CreateReplyObject(False, False)

                Dim strCurrentSubDir As String = parts(0)
                Dim strRelativePath As String = String.Empty
                For i As Integer = 1 To parts.Length - 1
                    strCurrentSubDir = System.IO.Path.Combine(strCurrentSubDir, parts(i))
                    strRelativePath = System.IO.Path.Combine(strRelativePath, parts(i))
                Next

                Dim clsTG As New Group("Directories")
                For Each clsSub As Entities.Infos.DirectoryInfo In EntityHandlers.SharedItems.GetAllSubdirectoriesByShareIdAndRelativeDirectoryAsInfos(share.Id, strRelativePath)
                    Dim clsDir As New Directory(IO.Path.Combine(directoryCode, clsSub.Name), clsSub.Name)
                    clsDir.TotalItems = clsSub.TotalItems
                    clsDir.TotalSize = clsSub.TotalSize
                    clsTG.Directories.Add(clsDir)
                Next
                clsRet.Groups.Add(clsTG)

                Dim clsGroup As New Group("Items")
                clsRet.Groups.Add(clsGroup)

                Dim aItems As Generic.List(Of Entities.SharedItem)
                aItems = EntityHandlers.SharedItems.GetAllByShareIdAndRelativeDirectory(share.Id, strRelativePath)
                clsGroup.Items = ConvertToItemInfos(aItems, False)
                Return clsRet
            End Function
            Private Shared Function GetRecent(ByVal share As Entities.Share, ByVal addedAfter As Date) As Reply
                Dim clsRet As Reply = CreateReplyObject(False, True)

                Dim clsGroup As New Group("Items")
                clsRet.Groups.Add(clsGroup)

                Dim aItems As New Generic.List(Of Entities.SharedItem)
                aItems = EntityHandlers.SharedItems.GetAllByShareIdAndAddedAfter(share.Id, addedAfter)
                clsGroup.Items = ConvertToItemInfos(aItems, True)
                Return clsRet
            End Function

            Private Shared Function ConvertToItemInfos(ByVal items As Generic.List(Of Entities.SharedItem), ByVal includeDirectory As Boolean) As Generic.List(Of ItemInfo)
                Dim aRet As New Generic.List(Of ItemInfo)
                For Each clsItem As Entities.SharedItem In items
                    Dim clsII As New ItemInfo
                    clsII.Id = clsItem.Id
                    clsII.Type = clsItem.ItemType
                    clsII.Name = clsItem.Name
                    clsII.UniqueId = clsItem.UniqueId
                    clsII.Cells.Add(New CellString(clsItem.Name))
                    clsII.Cells.Add(New CellFileSize(clsItem.TotalSize))
                    clsII.Cells.Add(New CellDate(clsItem.Added))
                    clsII.Cells.Add(New CellDate(clsItem.LastChanged))
                    If includeDirectory Then
                        clsII.Cells.Add(New CellString(clsItem.RelativeDirectory))
                    End If
                    aRet.Add(clsII)
                Next
                Return aRet
            End Function
            Private Shared Function CreateReplyObject(ByVal forFilters As Boolean, ByVal includeDirectory As Boolean) As Reply
                Dim clsRet As New Reply
                If forFilters Then
                    clsRet.Columns.Add(New Column("Name", 100, Column.HorizontalAlignment.Left))
                    clsRet.Columns.Add(New Column("Description", 250, Column.HorizontalAlignment.Left))
                Else
                    clsRet.Columns.Add(New Column("Name", 120, Column.HorizontalAlignment.Left))
                    clsRet.Columns.Add(New Column("Size", 60, Column.HorizontalAlignment.Right))
                    clsRet.Columns.Add(New Column("Added", 70, Column.HorizontalAlignment.Left))
                    clsRet.Columns.Add(New Column("Last Changed", 70, Column.HorizontalAlignment.Left))
                    If includeDirectory Then
                        clsRet.Columns.Add(New Column("Directory", 200, Column.HorizontalAlignment.Left))
                    End If
                End If
                Return clsRet
            End Function

        End Class
    End Class

End Namespace